package com.manolo.event.dispatcher;

import java.util.ArrayList;
import java.util.List;

import com.manolo.event.listener.*;
import com.manolo.event.*;

public class ChatDispatcher {
	private static ChatDispatcher instance;
	private List<ChatListener> externalListeners;
	private List<ChatListener> localListeners;

	private ChatDispatcher() {
		this.externalListeners = new ArrayList<ChatListener>();
		this.localListeners = new ArrayList<ChatListener>();
	}

	public synchronized static ChatDispatcher getInstance() {
		if(ChatDispatcher.instance == null) {
			ChatDispatcher.instance = new ChatDispatcher();
		}

		return ChatDispatcher.instance;
	}

	public void addExternalListener(ChatListener listener) {
		this.externalListeners.add(listener);
	}
	
	public void addLocalListener(ChatListener listener) {
		this.localListeners.add(listener);
	}

	public void removeExternalListener(ChatListener listener) {
		this.externalListeners.remove(listener);
	}
	
	public void removeLocalListener(ChatListener listener) {
		this.externalListeners.remove(listener);
	}

	public void dispatchChatEvent(ChatEvent event){
		this.dispatchChatEvent(event, DispatchType.EXTERNAL);
	}
	
	public void dispatchChatEvent(ChatEvent event, DispatchType type) {
		switch (type) {
			case EXTERNAL:
				for (ChatListener listener : this.externalListeners) {
					listener.GlobalChat(event);
				}
				break;
			case LOCAL:
				for (ChatListener listener : this.localListeners) {
					listener.GlobalChat(event);
				}
				break;
			case BOTH:
				for (ChatListener listener : this.externalListeners) {
					listener.GlobalChat(event);
				}
				for (ChatListener listener : this.localListeners) {
					listener.GlobalChat(event);
				}
				break;
		}
		
	}
	
	
}
